<?php

namespace App\Console\Commands\OpenShop\Nsq;


use App\Enums\Common\NsqEnum;
use App\Models\Common\ConfigModel;
use App\Remote\Feishu\Departments\DepartmentsRemote;
use App\Service\CallingCards\Rms\InvestmentEmployeeService;
use App\Service\OpenShop\ContractTemplateFeiShuService;
use App\Service\OpenShop\ContractTemplateService;
use App\Service\OpenShop\Feishu\FeishuApiService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubRmsFsEventCommand extends Command
{
    protected $signature = 'nsq:sub-rms-fs-event';

    protected $description = 'NSQ订阅rms飞书事件';

    public function handle()
    {
        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(
                NsqEnum::TOPIC_FS_EVENT, NsqEnum::CHANNEL_TIGER_FS_EVENT,
                function (Message $message) {
                    $this->doHandle($message->data());
                    $message->done();
                }
            );

//        $str = '';
//        $data = json_decode($str,true);
//        $this->doHandle($data);



    }

    /**
     * @param array $data
     * @doc https://open.feishu.cn/document/server-docs/approval-v4/event/common-event/approval-task-event
     * @return void
     */
    public function doHandle(array $data)
    {
        $eventType    = attributeGet($data, "header.event_type", null);
        $fsUserId     = attributeGet($data, "event.user_id", null);
        $fsUserName   = attributeGet($data, "event.name", null);

        Log::info('fsEvent_doHandle', $data);

        switch ($eventType) {
            case 'contact.user.created_v3'://通过该事件订阅员工入职。
//                InvestmentEmployeeService::addOne(['fsUserId' => $fsUserId, 'name' => $fsUserName]);
                break;

            case "contact.user.deleted_v3"://通过该事件订阅员工离职。
//                InvestmentEmployeeService::deleteOne($fsUserId);
                break;

        }

        $approval_code = attributeGet($data, "event.approval_code", null);
        $instanceCode  = attributeGet($data, "event.instance_code", null);
        $status        = attributeGet($data, "event.status", null);
        $appid         = attributeGet($data, "event.app_id", null);
        $type          = attributeGet($data, "event.type", null);

        switch ($approval_code){
            case ContractTemplateFeiShuService::FEI_SHU_APPROVAL_CODE_CONTRACT_TEMPLATE:
                // 加盟政策审批
                try {
                    Log::info('fsEvent_doHandle_contract_template', $data);

                    // 检查审批状态：
                    if ($status == "APPROVED" && $type == 'approval_task') {
                        $res = ContractTemplateService::feiShuCallback($appid, $instanceCode);
                        Log::info('fsEvent_doHandle_contract_template_res', $res);
                    }
                } catch (GuzzleException|ValidationException $e) {
                    Log::info('fsEvent_doHandle_ContractTemplateService_err', (array)$e->getMessage());
                }

                break;

            default:

                // other code

                break;


        }


    }
}
